//创建产品  即产品入库
exports.createProduct = (req, res) => {
	const {
		product_id,
		product_name,
		product_category,
		product_unit,
		product_inwarehouse_number,
		product_single_price,
		product_create_person,
		in_memo,
	} = req.body

	const product_create_time = new Date()
	const product_all_price = product_inwarehouse_number * 1 * product_single_price * 1
	const sql = 'insert into product set ?'
	db.query(sql, {
		product_id,
		product_name,
		product_category,
		product_unit,
		product_inwarehouse_number,
		product_single_price,
		product_all_price,
		product_create_person,
		product_create_time,
		in_memo,
	}, (err, result) => {
		if (err) return res.cc(err)
		res.send({
			status: 0,
			message: '添加产品信息成功'
		})
	})

}


//编辑产品信息
exports.editProduct = (req, res) => {
	const {
		product_name,
		product_category,
		product_unit,
		product_inwarehouse_number,
		product_single_price,
		in_memo,
		id
	} = req.body
	const product_update_time = new Date()
	const product_all_price = product_inwarehouse_number * 1 * product_single_price * 1
	const sql =
		'update product set product_name=?,product_category=?,product_unit=?,product_inwarehouse_number=?,product_single_price=?,product_all_price=?,product_update_time=?,in_memo=? where id=?'
	db.query(sql, {
		product_name,
		product_category,
		product_unit,
		product_inwarehouse_number,
		product_single_price,
		product_all_price,
		product_update_time,
		in_memo,
		id
	}, (err, result) => {
		if (err) return res.cc(err)
		res.send({
			status: 0,
			message: '编辑产品信息成功'
		})
	})

}

//获取产品列表
exports.getProductList = (req, res) => {
	const sql = 'select * from product where product_inwarehouse_number >= 1'
	db.query(sql, (err, result) => {
		if (err) return res.cc(err)
		res.send(result)
	})
}

//产品申请出库
exports.applyOutProduct = (req, res) => {
	const product_out_status = '申请出库'
	const {
		id,
		product_out_id,
		product_out_number,
		product_single_price,
		product_out_apply_person,
		apply_memo,
	} = req.body
	const product_apply_time = new Date()
	const product_out_price = product_out_number * 1 * product_single_price * 1
	const sql =
		'update product set product_out_status=?,product_out_id=?,product_out_number=?,product_out_price=?,product_out_apply_person=?,product_apply_time=?,apply_memo=? where id=?'
	db.query(sql, [
		product_out_status,
		product_out_id,
		product_out_number,
		product_out_price,
		product_out_apply_person,
		apply_memo,
		id
	], (err, result) => {
		if (err) return res.cc(err)
		res.send({
			status: 0,
			message: '申请出库成功'
		})
	})
}

//产品审核列表
exports.applyProductList = (req, res) => {
	const sql = 'select * from product where product_out_status not in ("同意")'
	db.query(sql, (err, result) => {
		if (err) return res.cc(err)
		res.send(result)
	})
}

//产品审核
exports.auditProduct = (req, res) => {
	const {
		id,
		product_out_id,
		product_out_status,
		audit_memo,
		product_out_price,
		product_out_audit_person,
		product_out_apply_person,
		product_inwarehouse_number,
		product_single_price,
		product_out_number,
	} = req.body
	const product_audit_time = new Date()
	if (product_out_status == '同意') {
		const newWarehouseNumber = product_inwarehouse_number * 1 - product_out_number * 1
		const product_all_price = newWarehouseNumber * 1 * product_single_price * 1
		const sql = 'insert into productout set ?'
		db.query(sql, {
			product_out_id,
			product_out_number,
			product_out_price,
			product_out_audit_person,
			product_out_apply_person,
			product_audit_time,
			audit_memo,

		}, (err, result) => {
			if (err) return res.cc(err)
			const sql1 =
				'update product set product_out_id=NULL,product_single_price=NULL,product_out_number=NULL,product_out_apply_person=NULL,apply_memo=NULL,product_out_price=NULL,product_apply_time=NULL  where id=?'
			db.query(sql1, req.body.id, (err, result) => {
				if (err) return res.cc(err)
				res.send({
					status: 0,
					message: '产品出库成功'
				})
			})

		})
	}
	if (product_out_status == '否决') {
		const sql = 'update product set audit_memo=? where id=?'
		db.query(sql, [audit_memo, id], (err, result) => {
			if (err) return res.cc(err)
			res.send({
				status: 0,
				message: '产品出库被否决'
			})
		})
	}

}